package com.vast.admin.entity;

import com.vast.admin.utils.AverageCapitalPlusInterestUtils;
import com.vast.admin.utils.AverageCapitalUtils;
import com.vast.admin.utils.BIAPPUtils;
import com.vast.system.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 贷款相关信息 及 还款计划
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Loan {

    private Long contractDetailId;

    /**
     * 借款期数
     */
    private Integer repaymentPeriod;

    /**
     * 月利率
     */
    private Double monthlyInterest;

    /**
     * 还款方式
     */
    private String repaymentMethod;

    /**
     * 每月还款日
     */
    private Integer monthlyRepaymentDate;

    /**
     * 合同金额
     */
    private Double contractAmount;

    /**
     * 还款计划
     */
    private List<RepaymentPlanWord> repaymentPlanWords;

    /**
     * 生成还款计划
     */
    public void generateRepaymentPlan() {
        //校验参数
        if (repaymentPeriod == null) {
            throw new BusinessException("借款期数为空，无法生成还款计划");
        } else if (monthlyInterest == null) {
            throw new BusinessException("月利率为空，无法生成还款计划");
        } else if (repaymentMethod == null) {
            throw new BusinessException("还款方式为空，无法生成还款计划");
        } else if (monthlyRepaymentDate == null) {
            throw new BusinessException("每月还款日为空，无法生成还款计划");
        } else if (contractAmount == null) {
            throw new BusinessException("合同金额为空，无法生成还款计划");
        }
        if (monthlyRepaymentDate > 31) {
            throw new BusinessException("每月还款日大于31，无法生成还款计划");
        }

        List<String> dateStr = new ArrayList<>(repaymentPeriod);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        for (int i = 1; i <= repaymentPeriod; i ++) {
            calendar.add(Calendar.MONTH, 1);
            if (calendar.getActualMaximum(Calendar.DAY_OF_MONTH) < monthlyRepaymentDate) {
                calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            } else {
                calendar.set(Calendar.DAY_OF_MONTH, monthlyRepaymentDate);
            }
            dateStr.add(sdf.format(calendar.getTime()));
        }

        if ("等额本金".equals(repaymentMethod)) {
            repaymentPlanWords = AverageCapitalUtils.cal(contractAmount, monthlyInterest, repaymentPeriod, dateStr);
        } else if ("等额本息".equals(repaymentMethod)) {
            repaymentPlanWords = AverageCapitalPlusInterestUtils.cal(contractAmount, monthlyInterest, repaymentPeriod, dateStr);
        } else if ("先息后本".equals(repaymentMethod)) {
            repaymentPlanWords = BIAPPUtils.cal(contractAmount, monthlyInterest, repaymentPeriod, dateStr);
        } else {
            throw new BusinessException("未知的还款方式，无法生成还款计划");
        }
    }


    public static void main(String[] args) {
        Loan loan = new Loan(null, 24, 0.001D, "先息后本", 10, 100000D, null);
        loan.generateRepaymentPlan();
//        int monthlyRepaymentDate = 15;
//        int repaymentPeriod = 24;
        System.out.println(loan.getRepaymentPlanWords());

    }
}
